package com.cover.fti;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.entity.scene.Scene.ITouchArea;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.input.touch.detector.ClickDetector;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasFactory;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureBuilder;
import org.anddev.andengine.opengl.texture.atlas.buildable.builder.ITextureBuilder.TextureAtlasSourcePackingException;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.apache.commons.logging.Log;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Debug;
import android.widget.Toast;

public class HelpActivity extends BaseGameActivity {

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	private Camera mCamera;
	private Scene mHelpScene;

	private BitmapTextureAtlas mBackgroundTextureAtlas;
	private TextureRegion mBackgroundTextureRegion;
	private BitmapTextureAtlas mButtontTextureAtlas;
	private TextureRegion mButtonHomeTextureRegion;
	private TextureRegion mButtonNextTextureRegion;
	private TextureRegion mButtonPrevTextureRegion;
	private TextureRegion mHelpTouchRegion;
	private TextureRegion mHelpDragRegion;
	private BitmapTextureAtlas mFontTextureAtlas;
	private BuildableBitmapTextureAtlas mHelpAtlas;
	private Font mFont;
	private ChangeableText textContent;
	private Sprite buttonPrevSprite;
	private Sprite buttonNextSprite;
	private Sprite helpTouch;
	private Sprite helpDrag;

	private int index = 0;
	private String helpString[] = new String[] {
			"Defend your Tower \nfrom enemy's attack, \nplace Tower behind the Wall \nand drop Bubble Bomb",
			"Drag and drop Tower \nuntil indicator color change \ninto Green",
			"Touch Screen to drop bomb",
			"Every Tower has different damage, \n If it has big damage, \nthen the attack speed of a small" };

	@Override
	public Engine onLoadEngine() {
		// TODO Auto-generated method stub
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
				this.mCamera));
	}

	@Override
	public void onLoadResources() {
		// TODO Auto-generated method stub
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mBackgroundTextureAtlas = new BitmapTextureAtlas(1024, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mBackgroundTextureAtlas, this,
						"menubg.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(mBackgroundTextureAtlas);
		this.mButtontTextureAtlas = new BitmapTextureAtlas(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mButtonHomeTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mButtontTextureAtlas, this,
						"buttonHome.png", 0, 0);
		this.mButtonNextTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mButtontTextureAtlas, this,
						"buttonNext.png", 64, 0);
		this.mButtonPrevTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mButtontTextureAtlas, this,
						"buttonPrev.png", 128, 0);

		this.mEngine.getTextureManager().loadTexture(mButtontTextureAtlas);
		this.mHelpAtlas = new BuildableBitmapTextureAtlas(512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mHelpTouchRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mHelpAtlas, this, "help_touch.png");
		this.mHelpDragRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mHelpAtlas, this, "help_drag.png");
		try {
			mHelpAtlas.build(new BlackPawnTextureBuilder(2));
		} catch (TextureAtlasSourcePackingException e1) {
			e1.printStackTrace();
		}
		this.mEngine.getTextureManager().loadTexture(mHelpAtlas);
		FontFactory.setAssetBasePath("font/");
		this.mFontTextureAtlas = new BitmapTextureAtlas(1024, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mFont = FontFactory.createFromAsset(this.mFontTextureAtlas, this,
				"BILLO.TTF", 28, true, Color.WHITE);
		this.mEngine.getTextureManager().loadTexture(this.mFontTextureAtlas);
		this.mEngine.getFontManager().loadFont(mFont);
	}

	@Override
	public Scene onLoadScene() {
		// TODO Auto-generated method stub
		this.mHelpScene = new Scene();
		Sprite background = new Sprite(
				(CAMERA_WIDTH - this.mBackgroundTextureRegion.getWidth()) / 2,
				(CAMERA_HEIGHT - this.mBackgroundTextureRegion.getHeight()) / 2,
				this.mBackgroundTextureRegion);
		this.mHelpScene.attachChild(background);
		
		Text textTitle = new Text(this.mCamera.getWidth() / 2, 50, this.mFont,
				"HELP");
		this.mHelpScene.attachChild(textTitle);
		textContent = new ChangeableText(110, 120, this.mFont, helpString[0]);
		mHelpScene.attachChild(textContent);
		helpDrag = new Sprite(100, 120, mHelpDragRegion);
		mHelpScene.attachChild(helpDrag);
		helpDrag.setVisible(false);
		helpTouch = new Sprite(100, 120, mHelpTouchRegion);
		mHelpScene.attachChild(helpTouch);
		helpTouch.setVisible(false);
		buttonPrevSprite = new Sprite(this.mButtonPrevTextureRegion.getWidth(),
				this.mCamera.getHeight()
						- this.mButtonPrevTextureRegion.getHeight(),
				this.mButtonPrevTextureRegion) {

			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {

				if (pSceneTouchEvent.isActionDown()) {
					index--;
					textContent.setVisible(false);
					if (index <= 0) {
						helpDrag.setVisible(false);
						helpTouch.setVisible(false);
						buttonPrevSprite.setVisible(false);
						textContent.setText(helpString[1]);
						textContent.setPosition(110, 120);
						index=0;

					} else if (index == 1) {
						helpTouch.setVisible(false);
						helpDrag.setVisible(true);
						textContent.setText(helpString[1]);
						textContent.setPosition(100 + helpDrag.getWidth(), 120);
						index=1;
					} else if (index == 2) {
						helpTouch.setVisible(true);
						helpDrag.setVisible(false);
						textContent.setText(helpString[2]);
						textContent.setPosition(100 + helpTouch.getWidth(), 120);
						index=2;
					}
					textContent.setVisible(true);
					buttonNextSprite.setVisible(true);
				}

				return true;
			}
		};
		this.mHelpScene.attachChild(buttonPrevSprite);
		this.mHelpScene.registerTouchArea(buttonPrevSprite);
		buttonPrevSprite.setVisible(false);
		buttonNextSprite = new Sprite(this.mCamera.getWidth()
				- this.mButtonNextTextureRegion.getWidth(),
				this.mCamera.getHeight()
						- this.mButtonNextTextureRegion.getHeight(),
				this.mButtonNextTextureRegion) {
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					index++;
					textContent.setVisible(false);
					if (index == 1) {
						helpTouch.setVisible(false);
						helpDrag.setVisible(true);
						textContent.setText(helpString[1]);
						textContent.setPosition(100 + helpDrag.getWidth(), 120);					
						index=1;
					} else if (index == 2) {
						helpTouch.setVisible(true);
						helpDrag.setVisible(false);
						textContent.setText(helpString[2]);
						textContent.setPosition(100 + helpTouch.getWidth(), 120);
						index=2;
					} else if (index>=3){
						helpTouch.setVisible(false);
						helpDrag.setVisible(false);
						textContent.setText(helpString[3]);
						textContent.setPosition(100, 120);
						buttonNextSprite.setVisible(false);
						index=3;
						
					}
					textContent.setVisible(true);
					buttonPrevSprite.setVisible(true);
				}
				return true;
			}
		};
		this.mHelpScene.attachChild(buttonNextSprite);
		this.mHelpScene.registerTouchArea(buttonNextSprite);

		Sprite buttonHomeSprite = new Sprite(CAMERA_WIDTH / 2, CAMERA_HEIGHT
				- this.mButtonHomeTextureRegion.getHeight(),
				this.mButtonHomeTextureRegion) {
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				Intent intent = new Intent(HelpActivity.this,
						BubbleDefenseActivity.class);
				HelpActivity.this.startActivity(intent);
				finish();
				return true;
			}
		};
		this.mHelpScene.attachChild(buttonHomeSprite);
		this.mHelpScene.registerTouchArea(buttonHomeSprite);
		this.mHelpScene.setTouchAreaBindingEnabled(true);
		return mHelpScene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}

}
